import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

import { NzModalService } from 'ng-zorro-antd/modal';

/**
 * 提示框
 * 
 * ```ts
    this.infoModalSrc
      .success('<b style="color: green;">这是成功信息(粗体,绿色)</b>')
      .subscribe(() => this.messageSrc.success('窗口关闭'));
			
 * ```
 *
 */
@Injectable({ providedIn: 'root' })
export class TipsService {
  constructor(private modalSrc: NzModalService) {}

  info<T>(content: string): Subject<T> {
    return this.common('信息提示', content, 'info');
  }

  warning<T>(content: string): Subject<T> {
    return this.common('警告提示', content, 'warning');
  }

  success<T>(content: string): Subject<T> {
    return this.common('成功提示', content, 'success');
  }

  fail<T>(content: string): Subject<T> {
    return this.common('失败提示', content, 'error');
  }

  private common<T>(title: string, content: string, type: string): Subject<T> {
		const data = {
      nzTitle: title,
      nzContent: content
    }

		if (type == 'info') {
			return this.modalSrc.info(data).afterClose;
		}
   
		if (type == 'warning') {
			return this.modalSrc.warning(data).afterClose;
		}
		
		if (type == 'success') {
			return this.modalSrc.success(data).afterClose;
		}
		
		if (type == 'error') {
			return this.modalSrc.error(data).afterClose;
		}		
    
		return this.modalSrc.info(data).afterClose;		
  }
}
